package com.gateway.plugin.security.uaa.authentication.client;

import com.gateway.security.common.authentication.client.ClientPrincipal;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;
import java.util.Map;

/**
 * @program: api-gateway
 * @description:
 * @author: YuKai Fan
 * @create: 2025/2/11 21:57
 **/
public class SecurityUser implements ClientPrincipal {

    /**
     * 用户名
     */
    private String name;

    /**
     * 租户id
     */
    private String tenantId;

    /**
     * 租户名称
     */
    private String tenantName;

    /**
     * 用户id
     */
    private String userId;

    /**
     * 参数属性
     */
    private Map<String, Object> attributes;

    /**
     * 权限信息
     */
    private Collection<? extends GrantedAuthority> authorities;

    public String getTenantId() {
        return tenantId;
    }

    public void setTenantId(String tenantId) {
        this.tenantId = tenantId;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    @Override
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public String getTenantName() {
        return tenantName;
    }

    public void setTenantName(String tenantName) {
        this.tenantName = tenantName;
    }

    public Map<String, Object> getAttributes() {
        return attributes;
    }

    public void setAttributes(Map<String, Object> attributes) {
        this.attributes = attributes;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public Object getPrincipal() {
        return this;
    }

    public void setAuthorities(Collection<? extends GrantedAuthority> authorities) {
        this.authorities = authorities;
    }


    public static final class Builder {
        private String name;
        private String tenantId;
        private String tenantName;
        private String userId;
        private Map<String, Object> attributes;
        private Collection<? extends GrantedAuthority> authorities;

        private Builder() {
        }

        public static Builder builder() {
            return new Builder();
        }

        public Builder name(String name) {
            this.name = name;
            return this;
        }

        public Builder tenantId(String tenantId) {
            this.tenantId = tenantId;
            return this;
        }

        public Builder tenantName(String tenantName) {
            this.tenantName = tenantName;
            return this;
        }

        public Builder userId(String userId) {
            this.userId = userId;
            return this;
        }

        public Builder attributes(Map<String, Object> attributes) {
            this.attributes = attributes;
            return this;
        }

        public Builder authorities(Collection<? extends GrantedAuthority> authorities) {
            this.authorities = authorities;
            return this;
        }

        public SecurityUser build() {
            SecurityUser securityUser = new SecurityUser();
            securityUser.setName(name);
            securityUser.setTenantId(tenantId);
            securityUser.setTenantName(tenantName);
            securityUser.setUserId(userId);
            securityUser.setAttributes(attributes);
            securityUser.setAuthorities(authorities);
            return securityUser;
        }
    }
}
